package grad.nxu.recruitsystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import grad.nxu.recruitsystem.domain.Seeker;
import grad.nxu.recruitsystem.service.SeekerService;
import grad.nxu.recruitsystem.vo.ApiResponse;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: HAPPY
 * @Project_name: recruitSystem
 * @Package_name: grad.nxu.recruitsystem.controller
 * @Date: 2023/3/7 16:08
 * @Description:
 */
@RestController
@RequestMapping("/api/v1/seeker")
public class SeekerController {
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    SeekerService seekerService;
    /**
     * 获取所有求职者信息
     * @param pageSize 每页多少条
     * @param pageNumber 第几页，从1开始
     * @return 返回获取结果
     */
    @ApiOperation("获取所有招聘者信息")
    @GetMapping("/admin/all")
    public ApiResponse<List<Seeker>> getAll(Integer pageSize, Integer pageNumber) {
        ApiResponse<List<Seeker>> apiResponse = new ApiResponse<>();
        Page<Seeker> page = new Page<>(pageNumber, pageSize);
        Page<Seeker> seekerPage;
        try {
            QueryWrapper<Seeker> queryWrapper = new QueryWrapper<>();
            seekerPage = seekerService.page(page, queryWrapper);
            for (Seeker seeker : seekerPage.getRecords()) {
                seeker.setPassword(null);
            }
            apiResponse.setCode(200).setSuccess(true).setData(seekerPage.getRecords());
        } catch (Exception e) {
            apiResponse.setCode(500).setSuccess(true).setMessage("服务器错误");
        }

        return apiResponse;
    }

    /**
     * 封号接口
     * @param id 求职者id
     * @return 返回封号结果
     */
    @ApiOperation("封号接口")
    @PutMapping("/admin/lock")
    public ApiResponse<String> lockSeeker(Integer id) {
        ApiResponse<String> apiResponse = new ApiResponse<>();
        try {
            UpdateWrapper<Seeker> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id", id);
            updateWrapper.set("status", false);
            seekerService.update(null, updateWrapper);
            apiResponse.setCode(200).setSuccess(true).setMessage("封禁账号成功");
        } catch (Exception e) {
            apiResponse.setCode(500).setSuccess(true).setMessage("服务器错误");
        }

        return apiResponse;
    }

    /**
     * 解封接口
     * @param id 求职者id
     * @return 返回封号结果
     */
    @ApiOperation("解封接口")
    @PutMapping("/admin/unLock")
    public ApiResponse<String> unLockSeeker(Integer id) {
        ApiResponse<String> apiResponse = new ApiResponse<>();
        try {
            UpdateWrapper<Seeker> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id", id);
            updateWrapper.set("status", true);
            seekerService.update(null, updateWrapper);
            apiResponse.setCode(200).setSuccess(true).setMessage("封禁账号成功");
        } catch (Exception e) {
            apiResponse.setCode(500).setSuccess(true).setMessage("服务器错误");
        }

        return apiResponse;
    }
}
